﻿using System.Text;


string alphabet = "абвгдеёжзийклмнопрстуфхцчшщъыьэюя ";

List<List<int>> countBigrams = [];
List<List<double>> logCountBigrams = [];
List<List<double>> frequencyBigrams = [];
List<List<double>> logFrequencyBigrams = [];
int allCountBigrams = 0;


for (int i = 0; i < 34; i++)
{
    countBigrams.Add(new List<int>(34));
    logCountBigrams.Add(new List<double>(34));
    frequencyBigrams.Add(new List<double>(34));
    logFrequencyBigrams.Add(new List<double>(34));
    for (int j = 0; j < 34; j++)
    {
        countBigrams[i].Add(0);
        logCountBigrams[i].Add(0);
        frequencyBigrams[i].Add(0);
        logFrequencyBigrams[i].Add(0);
    }
}

using FileStream fileText = new("C:\\Users\\khabi\\OneDrive\\Рабочий стол\\Programming\\Programs on C#\\Cryptography\\Конечные продукты\\CalculatingBigrams\\a.txt", FileMode.Open, FileAccess.Read);


int bytes = 1024;
byte[] bufferRead = new byte[bytes];

string Aij = "@#";
while (fileText.Read(bufferRead, 0, bytes) > 0)
{
    string str = (System.Text.Encoding.UTF8.GetString(bufferRead)).ToLower();

    Aij = Aij[0].ToString() + str[1];

    for (int i = 1; i < str.Length; i++)
    {
        Aij = Aij[1].ToString() + str[i];
        if (alphabet.Contains(Aij[0]) && alphabet.Contains(Aij[1]))
        {
            countBigrams[alphabet.IndexOf(Aij[0])][alphabet.IndexOf(Aij[1])]++;
            allCountBigrams++;
        }
    }
}

using FileStream fileCryptoText = new("C:\\Users\\khabi\\OneDrive\\Рабочий стол\\Programming\\Programs on C#\\Cryptography\\Конечные продукты\\CalculatingBigrams\\b.txt", FileMode.Open, FileAccess.Write);


// Таблица частот
var builder = new StringBuilder();
builder.Append(" \t\t\t");
foreach (char ch in alphabet)
{
    builder.Append(ch + "\t\t\t");
}
builder.Append('\n');

for (int i = 0; i < countBigrams.Count; i++)
{
    builder.Append(alphabet[i] + "\t");
    for (int j = 0; j < countBigrams[i].Count; j++)
    {
        frequencyBigrams[i][j] = (double)countBigrams[i][j] / allCountBigrams; //Math.Log(countBigrams[i][j] / allCountBigrams, 10);
        builder.Append(frequencyBigrams[i][j].ToString("F8") + "\t");
    }
    builder.Append('\n');
}

byte[] bufferWrite = System.Text.Encoding.UTF8.GetBytes(builder.ToString());

fileCryptoText.Write(bufferWrite, 0, bufferWrite.Length);

// Таблица логарифмов частот
builder = new StringBuilder();
builder.Append("\n\n Таблица логарифмов от частоты\n");

for (int i = 0; i < frequencyBigrams.Count; i++)
{
    builder.Append("\t\t\tnew double[] {\t");
    for (int j = 0; j < frequencyBigrams[i].Count; j++)
    {
        if (frequencyBigrams[i][j] > 0)
        {
            logFrequencyBigrams[i][j] = 10 * Math.Log(frequencyBigrams[i][j] * 100000000, 10);
        }
        
        builder.Append(logFrequencyBigrams[i][j].ToString("F2", System.Globalization.CultureInfo.GetCultureInfo("en-US")) + ",\t");
    }
    builder.Append("},\n");
}

bufferWrite = System.Text.Encoding.UTF8.GetBytes(builder.ToString());

fileCryptoText.Write(bufferWrite, 0, bufferWrite.Length);

// Таблица логарифмов количества
builder = new StringBuilder();
builder.Append("\n\n Таблица логарифмов от количества биграмм\n");

for (int i = 0; i < countBigrams.Count; i++)
{
    builder.Append("\t\t\tnew double[] {\t");
    for (int j = 0; j < countBigrams[i].Count; j++)
    {
        if (countBigrams[i][j] > 0)
        {
            logCountBigrams[i][j] = Math.Log(countBigrams[i][j], 10);
        }

        builder.Append(logCountBigrams[i][j].ToString("F2", System.Globalization.CultureInfo.GetCultureInfo("en-US")) + ",\t");
    }
    builder.Append("},\n");
}

bufferWrite = System.Text.Encoding.UTF8.GetBytes(builder.ToString());

fileCryptoText.Write(bufferWrite, 0, bufferWrite.Length);
